package system;

import weblogic.management.utils.*;

import java.io.IOException;

import java.net.MalformedURLException;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class ManageUser {

    private static MBeanServerConnection connection = null;
    private static ObjectName service = null;

    public ManageUser() {
        try {
            connection = ManageWeblogic.initConnection();
            service =
                    ManageWeblogic.getAuthenticationProviderObjectName(ManageWeblogic.MBEAN_INTERFACE);
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    /*
     * UserEditorMBean
     * */

    public void createUser(String username, String password,
                           String description) throws InstanceNotFoundException,
                                                      MBeanException,
                                                      ReflectionException,
                                                      IOException {
        connection.invoke(service, "createUser",
                          new Object[] { username, password, description },
                          new String[] { "java.lang.String",
                                         "java.lang.String",
                                         "java.lang.String" });
    }

    public boolean userExists(String username) throws InstanceNotFoundException,
                                                      MBeanException,
                                                      ReflectionException,
                                                      IOException {
        boolean checkExist =
            (Boolean)connection.invoke(service, "userExists", new Object[] { username },
                                       new String[] { "java.lang.String" });
        return checkExist;
    }

    public void removeUser(String username) throws InstanceNotFoundException,
                                                   MBeanException,
                                                   ReflectionException,
                                                   IOException {
        connection.invoke(service, "removeUser", new Object[] { username },
                          new String[] { "java.lang.String" });
    }

    public String getUserDescription(String username) throws InstanceNotFoundException,
                                                             MBeanException,
                                                             ReflectionException,
                                                             IOException {
        String userDescription =
            (String)connection.invoke(service, "getUserDescription",
                                      new Object[] { username },
                                      new String[] { "java.lang.String" });

        return userDescription;
    }

    public void setUserDescription(String username,
                                   String description) throws InstanceNotFoundException,
                                                              MBeanException,
                                                              ReflectionException,
                                                              IOException {
        connection.invoke(service, "setUserDescription",
                          new Object[] { username, description },
                          new String[] { "java.lang.String",
                                         "java.lang.String" });

    }

    public void advance(String username) throws InstanceNotFoundException,
                                                MBeanException,
                                                ReflectionException,
                                                IOException {
        connection.invoke(service, "advance", new Object[] { username },
                          new String[] { "java.lang.String" });
    }


    public void changeUserPassword(String username, String oldPassword,
                                   String newPassword) throws InstanceNotFoundException,
                                                              MBeanException,
                                                              ReflectionException,
                                                              IOException {
        connection.invoke(service, "changeUserPassword",
                          new Object[] { username, oldPassword, newPassword },
                          new String[] { "java.lang.String",
                                         "java.lang.String",
                                         "java.lang.String" });
    }
    public void resetUserPassword(String username,String newPassword) throws InstanceNotFoundException,
                                                              MBeanException,
                                                              ReflectionException,
                                                              IOException {
        connection.invoke(service, "resetUserPassword",
                          new Object[] { username, newPassword },
                          new String[] { "java.lang.String",
                                         "java.lang.String" });
    }
    public String listUsers() throws InstanceNotFoundException, MBeanException,
                                     ReflectionException, IOException {
        String strListUser =
            (String)connection.invoke(service, "listUsers", new Object[] { "*",
                                                                           100 },
                                      new String[] { "java.lang.String",
                                                     "java.lang.Integer" });
        return strListUser;
    }

    public String getCurrentName(String listUsers) throws InstanceNotFoundException,
                                                          MBeanException,
                                                          ReflectionException,
                                                          IOException {
        String user =
            (String)connection.invoke(service, "getCurrentName", new Object[] { listUsers },
                                      new String[] { "java.lang.String" });
        return user;
    }

    public boolean haveCurrent(String listUsers) throws InstanceNotFoundException,
                                                        MBeanException,
                                                        ReflectionException,
                                                        IOException {
        boolean haveCurrent =
            (Boolean)connection.invoke(service, "haveCurrent",
                                       new Object[] { listUsers },
                                       new String[] { "java.lang.String" });
        return haveCurrent;
    }
    /*
     * UserAttributeEditorMBean
     * */

    public void setUserAttributeValue(MBeanServerConnection connection,
                                      ObjectName service, String userName,
                                      String userAttributeName,
                                      Object newValue) throws InstanceNotFoundException,
                                                              MBeanException,
                                                              ReflectionException,
                                                              IOException,
                                                              Exception {
        connection.invoke(service, "setUserAttributeValue",
                          new Object[] { userName, userAttributeName,
                                         newValue },
                          new String[] { "java.lang.String",
                                         "java.lang.String",
                                         "java.lang.Object" });
    }

    public Object getUserAttributeValue(MBeanServerConnection connection,
                                        ObjectName service, String userName,
                                        String userAttributeName) throws InstanceNotFoundException,
                                                                         MBeanException,
                                                                         ReflectionException,
                                                                         IOException,
                                                                         Exception {
        Object obj =
            connection.invoke(service, "getUserAttributeValue", new Object[] { userName,
                                                                               userAttributeName },
                              new String[] { "java.lang.String",
                                             "java.lang.String" });
        return obj;
    }
}
